# -*- coding: utf-8 -*-
# @Time    : 2020/12/1 19:57
# @Author  :
# @FileName: flask_python.py
# @Software: PyCharm
# @Motto: study hard and day day u
import database.sqlserver_data_process as db
from flask import Flask, render_template, request, Response

app = Flask(__name__)

# 接受的数据变量

p = db.sqlserver_data_process()  # 创建一个对象


@app.route('/')
def index():
    # return render_template('accounts.html')
    # return render_template('products.html')
    return render_template('index.html', date_1="456")
    # return render_template('login.html')
    # return render_template('add-product.html')
    # return render_template('edit-product.html')


@app.route('/products.html')  # 查询所有订单信息【完】
def products_index():
    result = p.run_store_produce("EXEC find_all_factory_list")  # 订单号
    num_order = len(result)
    order_list = ""
    for i in result:
        index_ = 0
        for j in i:
            if index_ != 6:
                order_list = order_list + str(j) + ","
            index_ += 1
        order_list += "!"
    return render_template('products.html', order_num=num_order, order_list=order_list)


@app.route('/index.html')  # 首页显示信息【完】
def index_index():
    result = p.run_store_produce("EXEC get_whole_product_fac")  # 第一张利润表数据

    DATE1 = str(result[0][0])[5:]
    DATE2 = str(result[1][0])[5:]
    DATE3 = str(result[2][0])[5:]
    DATE4 = str(result[3][0])[5:]
    DATE5 = str(result[4][0])[5:]
    DATE6 = str(result[5][0])[5:]
    DATE7 = str(result[6][0])[5:]

    # 销量
    sale_1 = str(result[0][1])
    sale_2 = str(result[1][1])
    sale_3 = str(result[2][1])
    sale_4 = str(result[3][1])
    sale_5 = str(result[4][1])
    sale_6 = str(result[5][1])
    sale_7 = str(result[6][1])

    print(result)
    item_num1 = p.run_store_produce("EXEC find_all_class_item_fac_1")[0][0]
    item_num2 = p.run_store_produce("EXEC find_all_class_item_fac_2")[0][0]
    item_num3 = p.run_store_produce("EXEC find_all_class_item_fac_3")[0][0]
    item_num4 = p.run_store_produce("EXEC find_all_class_item_fac_4")[0][0]
    item_num5 = p.run_store_produce("EXEC find_all_class_item_fac_5")[0][0]

    if item_num1 is None:
        item_num1 = 0
    if item_num2 is None:
        item_num2 = 0
    if item_num3 is None:
        item_num3 = 0
    if item_num4 is None:
        item_num4 = 0
    if item_num5 is None:
        item_num5 = 0
    bad_num = p.run_store_produce("EXEC find_all_bad_item_fac")
    total_num = p.run_store_produce("EXEC find_all_item_fac")

    result = p.run_store_produce("EXEC find_all_factory_list")  # 订单号
    num_order = len(result)
    order_list = ""
    for i in result:
        for j in i:
            order_list = order_list + str(j) + ","
        order_list += "!"

    return render_template('index.html', DATE1=DATE1, DATE2=DATE2, DATE3=DATE3, DATE4=DATE4,
                           DATE5=DATE5, DATE6=DATE6, DATE7=DATE7, sale_1=sale_1,
                           sale_2=sale_2, sale_3=sale_3, sale_4=sale_4, sale_5=sale_5,
                           sale_6=sale_6, sale_7=sale_7, item_num1=item_num1, item_num2=item_num2, item_num3=item_num3,
                           item_num4=item_num4, item_num5=item_num5, bad_num=bad_num[0][0], total_num=total_num[0][0],
                           order_num=num_order, order_list=order_list)


@app.route('/accounts.html')
def accounts_index():
    return render_template('accounts.html')


@app.route('/login.html')
def login_index():
    return render_template('login.html')


@app.route('/adjust.html')  # 工厂信息【完】【要再加一个模糊查询】
def adjust_index():
    result = p.run_store_produce("EXEC find_all_factory_info_fac")  # 订单号
    num_order = len(result)
    order_list = ""
    for i in result:
        for j in i:
            order_list = order_list + str(j).replace('None', '未停业整顿') + ","
        order_list += "!"

    return render_template('adjust.html', order_num=num_order, order_list=order_list)


@app.route('/getFactoryInfo')  # 插入工厂【完】
def get_Factory():
    Factory_data_name = request.args.get('name')
    Factory_data_address = request.args.get('address')

    p.run_store_produce("EXEC insert_factory '{}', '{}'".format(Factory_data_name, Factory_data_address))
    return Response(adjust_index())


@app.route('/delete_factory')  # 删除工厂【完】
def delete_factory():
    Factory_data_name = request.args.get('delete_factory_name')
    p.run_store_produce("EXEC delete_factory '{}'".format(Factory_data_name))
    return Response(adjust_index())


@app.route('/getOrderInfo')  # 模糊查询订单信息【完】
def get_Order():
    data1 = request.args.get('shop')
    result = p.run_store_produce("EXEC find_all_factory_info_fac_mohu '{}'".format(data1))  # 订单号
    num_order = len(result)
    order_list = ""
    for i in result:
        index_ = 0
        for j in i:
            if index_ != 6:
                order_list = order_list + str(j) + ","
            index_ += 1
        order_list += "!"

    return render_template('products.html', order_num=num_order, order_list=order_list, temp_value=data1)


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5670)
